<?php

/**
 * This is the model class for table "{{page}}".
 *
 * The followings are the available columns in table '{{page}}':
 * @property integer $id
 * @property string $name
 * @property string $title
 * @property integer $position
 * @property integer $parent_id
 * @property integer $layout_id
 * @property string $created
 * @property string $updated
 */
class Page extends CActiveRecord
{
	private $_nodeList;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Page the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{page}}';
	}
	
	public function behaviors(){
		return array(
			'CAdvancedArBehavior' => array(
				'class' => 'ext.CAdvancedArBehavior',
			),
			'ParentChildTreeBehavior' => array(
				'class' => 'application.components.ParentChildTreeBehavior',
			),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, layout_id, title, in_navigation', 'required'),
            array('position, parent_id, layout_id', 'numerical', 'integerOnly'=>true),
            array('in_navigation', 'boolean'),
            array('name, title', 'length', 'max'=>50),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, position, parent_id, layout_id, title, created, updated, nodes', 'safe', 'on'=>'search'),
        );
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'layout'=>array(self::BELONGS_TO, 'Layout', 'layout_id'),
            'nodes'=>array(self::MANY_MANY, 'Node',
                '{{page_node}}(page_id, node_id)'),
		);
	}
	
	public function scopes(){
		return array(
			'inNavigation'=>array('condition'=>'in_navigation=1'),
		);
	}
	
	/**
	 * named scope
	 * @param integer $id
	 */
	public function parentIs($id){
		if(is_null($id)){
			$this->getDbCriteria()->mergeWith(array(
				'condition'=>'parent_id IS NULL',
			));
		} else {
			$this->getDbCriteria()->mergeWith(array(
				'condition'=>'parent_id='.$id,
			));
		}
	    return $this;
	}
	
	public static function navigation($parentId){
		$models = Page::model()->parentIs($parentId)->inNavigation();
		$models->getDbCriteria()->mergeWith(array(
			'order'=>'position',
		));
		return $models;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('back', 'ID'),
			'name' => Yii::t('back', 'Name'),
			'title' => Yii::t('back', 'Title'),
			'position' => Yii::t('back', 'Position'),
			'parent_id' => Yii::t('back', 'Parent Page'),
			'in_navigation' => Yii::t('back', 'In Navigation'),
			'layout_id' => Yii::t('back', 'Layout'),
			'created' => Yii::t('back', 'Created'),
			'updated' => Yii::t('back', 'Updated'),
			'nodes.name' => Yii::t('back', 'Nodes'),
			'layout.name' => Yii::t('back', 'Layout'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('position',$this->position);
        $criteria->compare('parent_id',$this->parent_id);
        $criteria->compare('layout_id',$this->layout_id);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('created',$this->created,true);
        $criteria->compare('updated',$this->updated,true);
        
		$criteria->compare('node_id',$this->nodes);
		$criteria->with=array('nodes');
		$criteria->together=true;

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * @return string comma seperated list of all node names related to page
	 */
	public function getNodeList(){
		if(isset($_nodeList)){
			return $_nodeList;
		}
		if(is_array($this->nodes)){
			$out=CHtml::listData($this->nodes,'id','name');
			$_nodeList = implode(', ', $out);
		} else {
			$_nodeList = '';
		}
		return $_nodeList;
	}
	
	public function setNodeList($nodeList){
		return;
		//$_nodeList = $nodeList;
	}
	
	public function getUrl(){
        return Yii::app()->createUrl('page/view', array(
            'name'=>$this->name,
        ));
	}
	
	
}